// pages/account/account.js
let utils = require("../../utils/utils.js");
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
  data: {
    nodata: false,
    account: [],
    accountnum: [],
    /* 判断每次加载数据长度小于10阻止加载 */
    offset: 0,
    moreaccount: false
    , bottom: false
    , nonOrder: true
    , walletlist: []
    , walletInfo: ""
    , dateInfo: { minHour: 10, maxHour: 20, minDate: new Date(), maxDate: new Date(2019, 10, 1), currentDate: new Date() },
    getMywalletPledgeUrl: '/myWallet/myBalance',
    getWalletPledgeListUrl: '/myWallet/balanceRecord',
    rechargeUrl: '/myWallet/recharge'
    , rechargePayUrl: '/myWallet/rechargePay',
    show: false,
    ruleInfos: [],
    rechageId: '',
    requestOne: false,
    nodata: false,
    walletlistnum: [],/* 判断每次加载数据长度小于10阻止加载 */
    morewalletlist: false,
    isShow: false,
	cardBglastImg: '/walletbg.png'
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad(options) {

  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady() {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow() {
    let that = this;
    that.getWalletData();
    let offset = that.data.offset = 0;
    that.getListData(offset)
	this.img();
  },
  img() {
  	let emptyImg = app.globalData.baseimg + this.data.emptyLastImg;
  	let cardBgImg = app.globalData.baseimg + this.data.cardBglastImg;
  	this.setData({
  		emptyImg,
  		cardBgImg,
  	});
  },
  /* 微信下拉到底刷新 */
  onReachBottom() {
    let that = this;
    let offset = that.data.offset;
    let walletlistnum = that.data.walletlistnum;
    if (that.data.walletlistnum.length < 10) {
      that.setData({
        morewalletlist: true
      });
      return false;
    }
    offset++;
    that.data.offset = offset;
    that.getListData(offset)
  },
  getWalletData: function () {
    let that = this;
    let indexData = wx.getStorageSync('indexData');
    let _data = {
      url: that.data.getMywalletPledgeUrl,
      methods: 'post',
      nonelist: true,
      data: {
        userId: indexData.user.userId,
        type: indexData.user.type
      }
    };
    utils.request.call(_data, (res) => {
      console.log(res)
      if (res.data.status == 200) {
        let walletBalance = res.data.data;
        if (!walletBalance) {
          walletBalance = '0.00';
        } else {
          walletBalance = res.data.data;
        };
        that.setData({
          wallerInfo: walletBalance,
        })
      };
    });
  },
  getListData: function (offset) {
    let that = this;
    let indexData = wx.getStorageSync('indexData');
    let _data = {
      url: that.data.getWalletPledgeListUrl,
      methods: 'post',
      nonelist: true,
      data: {
        userId: indexData.user.userId,
        type: indexData.user.type,
        offset: offset
      }
    };
    utils.request.call(_data, (res) => {
      console.log(res)
      let walletlist = res.data.data.list.content;
      that.data.walletlistnum = walletlist;
      if (walletlist.length == 0) {
        if (offset == 0) {
          that.data.walletlist = walletlist;
          that.setData({
            isShow: true,
            walletlist: walletlist,
            nodata: true
          })
        } else {
          let walletlistmore = that.data.walletlist.concat(walletlist);
          that.data.walletlist = walletlistmore;
          that.setData({
            isShow: true,
            walletlis: walletlistmore,
            morewalletlist: true
          })
        }
      } else {
        if (offset == 0) {
          that.data.walletlist = walletlist;
          that.setData({
            isShow: true,
            walletlist: walletlist
          })
        } else {
          let walletlistmore = that.data.walletlist.concat(walletlist);
          that.data.walletlist = walletlistmore;
          that.setData({
            isShow: true,
            walletlist: walletlistmore,
            morewalletlist: true
          })
        }
      }
    });
  },
  recharge: function () {
    this.getRechargeData();
  },
  /*点击取消充值*/
  cancelRecharge: function () {
    this.data.show = false;
    this.setData({
      show: this.data.show
    })
  },
  getRechargeData: function () {
    let that = this;
    let _data = { url: that.data.rechargeUrl, data: '' };
    utils.request.call(_data, (res) => {
      console.log(res)
      if (res.data.status == 200) {
        that.data.ruleInfos = res.data.data;
        that.data.show = true;
      };
      that.setData({
        show: that.data.show,
        ruleInfos: that.data.ruleInfos
      })
    });
  },
  chargePay: function (e) {
    let that = this;
    that.data.rechageId = e.currentTarget.dataset.id;
    that.data.show = false;
    that.data.requestOne = false;
    that.common_request_post(function (res) {
      console.log(res)
      let param = res.data.data;
      that.onBridgeReadyArgments(param);
    });
    that.setData({
      show: that.data.show,
      rechageId: that.data.rechageId
    })
  },
  common_request_post: function (success) {
    let that = this;
    let indexData = wx.getStorageSync('indexData');
    let userData = indexData.user;
    console.log(userData)
    let requestUrl = that.data.requestOne ? that.data.getMywalletPledgeUrl : that.data.rechargePayUrl;
    let _data = {
      url: that.data.rechargePayUrl,
      methods: 'post',
      nonelist: true,
      data: { 'userId': userData.userId, 'type': userData.type, 'deviceCode': userData.deviceCode, 'rechargeRuleId': that.data.rechageId }
    };
    utils.request.call(_data, (res) => {
      console.log(res)
      success(res);
    });
  },
  /*发起微信支付*/
  onBridgeReadyArgments: function (param) {
    let that = this;
    console.log(param);
	let wxpay=true;
	wx.setStorageSync('wxpay',wxpay);
    wx.requestPayment({
      timeStamp: param.timeStamp,
      nonceStr: param.nonceStr,
      package: param.package,
      signType: param.signType,
      paySign: param.paySign,
      success: function (res) {
        if (res.errMsg == "requestPayment:ok") { // 调用支付成功
          that.data.requestOne = true;
          that.common_request_post(function (res) {
            if (res.data.status == 200) {
              utils.tips('支付成功');
              setTimeout(function () {
                that.getWalletData();
                let offset = that.data.offset = 0;
                that.getListData(offset)
              }, 800);
			  wx.removeStorageSync('wxpay');
            } else {
              utils.tips(res.data.msg);
			  wx.removeStorageSync('wxpay');
            };
          });
        } else if (res.errMsg == 'requestPayment:cancel') {
          utils.tips('支付失败！');
		  wx.removeStorageSync('wxpay');
          return false;
        };
      },
      fail: function (res) {
        utils.tips('您已取消支付！');
		wx.removeStorageSync('wxpay');
        //fail
      },
      complete: function () {
        //complete
		wx.removeStorageSync('wxpay');
      }
    });
  },
	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide() {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload() {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh() {

  },

	/**
	 * 页面上拉触底事件的处理函数
	 */

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage() {

  }
})
